package com.senken.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.senken.base.BaseController;
import com.senken.commons.shiro.ShiroUser;
import com.senken.service.ISkUserService;

@Controller
public class LoginController extends BaseController{

private static final Logger LOGGER = LogManager.getLogger(LoginController.class);


     @Autowired
     private ISkUserService userservice;
	/**
     * 首页
     *
     * @return
     */
    @RequestMapping(value = "/")
    public String index() {

        return "redirect:/index";
    }
    /**
     * 首页
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/index")
    public String index(Model model) {
    	 if (SecurityUtils.getSubject().isAuthenticated()) {
             return "index";
         }
         return "login";
    }
    
    @RequestMapping(value = "/upload/{a}")
    public String upload(String a){
    	return a;
    }
    
    
    
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Object loginPost(String username, String pwd) {
        LOGGER.info("POST请求登录");
        Subject user = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, pwd.toCharArray());
        // 默认设置为记住密码，你可以自己在表单中加一个参数来控制
        token.setRememberMe(true);
        try {
            user.login(token);
        } catch (UnknownAccountException e) {
            LOGGER.error("账号不存在！", e);
            return renderError("账号不存在");
        } catch (DisabledAccountException e) {
            LOGGER.error("账号未启用！", e);
            return renderError("账号未启用");
        } catch (IncorrectCredentialsException e) {
            LOGGER.error("密码错误！", e);
            return renderError("密码错误");
        } catch (RuntimeException e) {
            LOGGER.error("未知错误,请联系管理员！", e);
            return renderError("未知错误,请联系管理员");
        }
        ShiroUser user1 = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
        userservice.updatelogintime(user1.loginName);
        return renderSuccess();
    }
    /**
     * 退出
     * @return {Result}
     */
    @RequestMapping(value = "/logout")
    public String logout() {
        LOGGER.info("登出");
        System.out.println("登出");
        Subject subject = SecurityUtils.getSubject();
        ShiroUser user = (ShiroUser) subject.getPrincipal();
        userservice.updatelogoutime(user.loginName);
        subject.logout();
        return "login";
    }
}
